<!--
 * @Descripttion: 
 * @version: 
 * @Author: mmzhang
 * @Date: 2021-07-22 14:55:59
 * @LastEditTime: 2023-01-06 11:28:20
-->
<template>
  <div class="refund-order">
    <dt-search ref="search" :labelWidth="100" :xl="5" :xlBtn="10" @reset="resetFun">
      <Button type="primary" @click="exportFun(0)" v-if="unlocking('0104040102')" style="margin-right: 8px">
        导出
      </Button>
      <Button type="primary" @click="exportFun(1)" v-if="unlocking('0104040103')">全号导出1</Button>
      <Button type="primary" style="margin-right: 8px">
        <a
          href="https://images.tospurfang.com/doc/%E9%80%80%E7%AD%BE%E5%8D%95%E6%93%8D%E4%BD%9C%E8%AF%B4%E6%98%8E.pdf"
          style="color: #fff"
          target="_blank">
          查看操作指引
        </a>
      </Button>
      <Button type="primary" @click="operationClick" v-if="unlocking('0104040104')">查看导出记录</Button>
    </dt-search>
    <!--统计信息-->
    <order-count ref="orderCount"></order-count>
    <dt-grid ref="table" :page="page" :onFetch="onFetch" />
    <!--客户详情-->
    <Modal v-model="detailsFlag" title="客户详情" width="80%" :styles="{ top: '5%' }" :footer-hide="true">
      <detail-page v-if="detailsFlag" :detailsParams="detailsParams"></detail-page>
    </Modal>
    <operation-record ref="operationRecord"></operation-record>
  </div>
</template>
<script>
  import RefundColumn from './refundOrderColumn'
  import searchOpt from './search'
  import detailPage from '../../allCustomersManagement/details.vue'
  import dt from '@dt/dt'
  import refundMgr from '@/biz/sales/refundOrder'
  import subCommonMgr from '@/biz/sales/salesCommon'
  import orderCount from '../components/orderCount.vue'
  let filter = null
  export default {
    data() {
      return {
        detailsFlag: false,
        detailsParams: {},
        searchInfo: {
          housesId: dt.buildInfo().housesId,
          businessResult: '',
          backCode: '',
          orderCode: '',
          productIds: [], //产品
          buildingIds: [],
          unitIds: []
        },
        page: {
          rows: 50,
          current: 1
        },
        filterParams: {},
        countList: [
          {
            title: '退签金额(元)',
            key: 'contractBackMoney'
          },
          {
            title: '退签套数(套)',
            key: 'contractBackCount'
          },
          {
            title: '退签面积(㎡)',
            key: 'contractBackArea'
          }
        ],
        countVal: {
          contractArea: 0,
          contractBackArea: 0,
          contractBackCount: 0,
          contractBackMoney: 0,
          contractCount: 0,
          contractMoney: 0,
          repaymentCount: 0,
          repaymentMoney: 0,
          subscriptionArea: 0,
          subscriptionBackArea: 0,
          subscriptionBackCount: 0,
          subscriptionBackMoney: 0,
          subscriptionCount: 0,
          subscriptionMoney: 0
        },
        tips: '汇总数据不包含待审核及已驳回数据'
      }
    },
    beforeRouteLeave(to, from, next) {
      if (to.name === 'refundOrderDetail') {
        this.retainParamsFun()
      }
      next()
    },
    components: {
      detailPage,
      orderCount
    },
    created() {},
    mounted() {
      let _this = this

      let retainParams = dt.memory.get('refundOrder_params')

      this.$refs.table.init(this, new RefundColumn(this))
      this.$refs.search.init(this.$refs.table, new searchOpt(this).options(), this.searchInfo)

      this.$nextTick(function () {
        if (retainParams) {
          Object.keys(this.searchInfo).map(function (key) {
            retainParams[key] && (_this.searchInfo[key] = retainParams[key]) //比较key
          })
          retainParams.rows && (this.page.rows = retainParams.rows)
          retainParams.page && (this.page.current = retainParams.page)
        }
        filter && _this.$refs.table.throwFilter().import(filter)
      })
      this.$refs.table.fetch()
    },
    methods: {
      operationClick() {
        this.$refs.operationRecord.show('export_refundOrder', 'M_15', this.$dt.buildInfo().housesId)
      },
      resetFun() {
        dt.memory.remove('refundOrder_params')
        this.$refs.search.init(this.$refs.table, new searchOpt(this).options(), this.searchInfo)
      },
      onFetch(data) {
        this.filterParams = {
          ...data
        }

        this.getOrderStatistics()

        return refundMgr
          .getRefundOrderList(data)
          .then((res) => {
            return res
          })
          .catch((err) => {})
      },
      getOrderStatistics() {
        let params = { ...this.filterParams }
        params.orderType = 'CONTRACT_BACK'
        params.statusList = params.businessResult ? [params.businessResult] : []
        params.productIds = params.productId ? [params.productId] : []
        subCommonMgr.getOrderStatistics(params).then((res) => {
          this.countVal = { ...res }
          this.$refs.orderCount.initData(this.tips, this.countList, this.countVal, false)
        })
      },

      retainParamsFun() {
        dt.memory.set('refundOrder_params', this.filterParams)
        filter = this.$refs.table.throwFilter().export()
      },
      // 客户姓名链接到客户资料
      onLinkUser(row) {
        if (row.customerName == '-') {
          return false
        }
        this.detailsFlag = true
        this.detailsParams = {
          customerId: row.customerId, // 手机号唯一编码
          userCustomerId: row.userCustomerId, // 客户信息唯一编码
          buildingId: dt.buildInfo().housesId,
          userId: dt.userInfo().userId,
          cityCode: dt.buildInfo().cityId
        }
      },
      detailFun(params) {
        let orderParams = {}
        let row = params[0]
        let type = params[1]
        switch (type) {
          case 'QY':
            orderParams = {
              orderNum: row.contractCode, // 单号
              businessId: row.contractId, // 业务id
              buildingId: this.$dt.buildInfo().housesId,
              subscriptionId: ''
            }
            break
          case 'UQ':
            orderParams = {
              orderNum: row.backCode, // 单号
              businessId: row.backId, // 业务id
              buildingId: this.$dt.buildInfo().housesId,
              subscriptionId: ''
            }
            break
          default:
            break
        }
        this.linkOrderFun(orderParams)
      },
      // 导出
      exportFun(exportType) {
        let params = {
          ...this.filterParams
        }
        params.exportType = exportType
        refundMgr
          .exportRefund(params)
          .then((res) => {})
          .catch((err) => {})
      }
    }
  }
</script>
<style lang="scss" scoped>
  .refund-order {
    padding: 20px;

    .refund-total {
      line-height: 32px;
      color: #d9001b;
    }

    .count-content {
      margin-bottom: 8px;
      padding: 5px 20px;
      background-color: #e8eaec;

      li {
        margin-right: 2%;

        p {
          line-height: 32px;
        }

        .num {
          text-align: center;
          font-size: 20px;
          font-weight: bold;
          color: #222222;
        }

        .text {
          font-size: 12px;
          color: #666;
          text-align: center;
        }
      }
    }
  }
</style>
